package com.changgou;

import com.changgou.order.config.CartMQProperties;
import com.changgou.order.config.MqProperties;
import entity.IdWorker;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import tk.mybatis.spring.annotation.MapperScan;

/**
 * @author ljh
 * @version 1.0
 * @date 2021/1/5 11:49
 * @description 标题
 * @package com.changgou
 */
@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages = "com.changgou.order.dao")
@EnableFeignClients(basePackages = {"com.changgou.goods.feign","com.changgou.user.feign"})
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

    @Bean
    public IdWorker idWorker(){
        return new IdWorker(0,1);
    }

    //获取yml中的属性对应的值
    @Autowired
    private Environment environment;


    //创建队列
    @Bean
    public Queue queueOrder(){
        return new Queue(environment.getProperty("mq.pay.queue.order"));
    }

    //创建交互机 路由模式的交换机
    @Bean
    public DirectExchange createExchange(){
        return new DirectExchange(environment.getProperty("mq.pay.exchange.order"));
    }

    //创建绑定 指定routingkey
    @Bean
    public Binding createBinding(){
        return BindingBuilder.bind(queueOrder()).to(createExchange()).with(environment.getProperty("mq.pay.routing.key"));
    }
    //============================================================cart=====================

    @Autowired
    private CartMQProperties cartMQProperties;

    @Autowired
    private MqProperties mqProperties;

    @Bean
    public Queue queueCartOrder(){
        return new Queue(cartMQProperties.getQueue());
    }

    //创建交互机 路由模式的交换机
    @Bean
    public DirectExchange createCartExchange(){
        return new DirectExchange(cartMQProperties.getExchange());
    }

    //创建绑定 指定routingkey
    @Bean
    public Binding createCartBinding(){
        return BindingBuilder.bind(queueCartOrder()).to(createCartExchange()).with(cartMQProperties.getRouting());
    }

    //=========================================cart=====================

    //开启分布式事务是在要事务开启端
    /*@Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }

    @Primary
    @Bean("dataSourceProxy")
    public DataSourceProxy dataSourceProxy(DataSource dataSource) {
        //AT模式
        return new DataSourceProxy(dataSource);
    }*/
}
